package com.example.test;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import android.util.Log;

import com.example.fmgidsprototype.data.DataManager;
import com.example.fmgidsprototype.data.VenueCategoryKey;
import com.example.fmgidsprototype.model.Category;
import com.example.fmgidsprototype.model.Venue;
import com.example.fmgidsprototype.model.VenueLocation;
import com.example.fmgidsprototype.webservice.I_CallBack;
import com.example.fmgidsprototype.webservice.n_cst_categories;
import com.example.fmgidsprototype.webservice.n_cst_category;
import com.example.fmgidsprototype.webservice.n_cst_venue;

public class n_cst_category_venues implements I_CallBack {
	
	private DataManager dm;
	
	public n_cst_category_venues(DataManager dm){
		this.dm = dm;
	}
	
	@Override
	public void of_call_back(Object anv_return) {
		// TODO Auto-generated method stub
		n_cst_categories lnv_categories;
		
		if (anv_return == null)
		{
			Log.d("n_cst_category_venues", "nothing returned");	
		}
		else
		{
			lnv_categories = (n_cst_categories) anv_return;
			List<Category> catList = new ArrayList<Category>();
			for(n_cst_category c : lnv_categories.getCategories()){
				Category cat = new Category();
				cat.setName(c.getName());
				cat.setDescription(c.getDescription());
				cat.setIconUrl(c.getIconName());
				
				Set<Venue> venueSet = new HashSet<Venue>();
				List<n_cst_venue> catVenues = c.getVenues();
				for(n_cst_venue v : catVenues){
					Venue venue = new Venue();
					venue.setJsonId(v.getJsonID());
					venue.setName(v.getName());
					venue.setDescription(v.getDescription());
					venue.setAddress(v.getAddress_nl());
					venue.setThumbName(v.getThumbName());
					venue.setPicUrl(v.getImageUrl());
					venue.setUrl(v.getUrl());
					venue.setEmail(v.getEmail());
					venue.setPhone(v.getPhone());
					venue.setAudioPath(v.getAudioPath());
					venue.setLatitude(Double.valueOf(v.getLatitude()));
					venue.setLongitude(Double.valueOf(v.getLongitude()));
					venue.setDistanceFromZero(Double.valueOf(v.getDistanceFromPointZero()));
					venue.setRating(Double.valueOf(v.getRating()));
					venueSet.add(venue);
				}
				cat.setVenues(venueSet);
				catList.add(cat);
			}
			this.writeToDb(catList);
			Log.d("n_cst_category_venues", lnv_categories.getModifiedDate());
		}		
	}
	
	private void writeToDb(List<Category> categories){
		for(Category category : categories){
			//first store category
			long catId;
			if((catId = dm.updateCategory(category)) <= 0)
				catId = dm.saveCategory(category);
			//iterate over venues for this cat
			Iterator<Venue> it = category.getVenues().iterator();
			//for each one...
			while(it.hasNext()){
				
				Venue v = it.next();
				//store it
				long id;
				if((id = dm.updateVenue(v)) <= 0)
					id = dm.saveVenue(v);
				//store the location of the venue if it is a new venue
				VenueLocation venueLocation = new VenueLocation(
							id, v.getLongitude(), v.getLatitude(), v.getDistanceFromZero());
					if(dm.updateVenueLocation(venueLocation) <= 0)
						dm.saveVenueLocation(venueLocation);
				VenueCategoryKey key = new VenueCategoryKey(id, catId);
				if(dm.updateKey(key) <= 0)
					dm.saveKey(key);
			}
		}
	}
}
